package Gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JOptionPane;
import Logic.LoginInfo;
import client.LibaryMain;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * this class runing the main tra
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class Viewlending extends JPanel {
	
	JTable table;
	String bookid,subid;
	/**
	 * view lending constractor 
	 * @param str
	 */
	public Viewlending(String [] str) {
		setBackground(new Color(153, 204, 255));
		init(str);

	}
	/**
	 * init the view lending window 
	 * @param str
	 */
			
	public void init (String [] str) {
		 Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		 

	        for (int i=1; i<=(str.length-3);i=i+3){
	        Vector<Object> row = creatrow(str,i);
	        subid=str[2];
	        data.add(row);
	        }
		
			System.out.println(">>>>>filling table>>>>>>>>");
	        Vector<String> headers = new Vector<String>();
	      
	     
		     headers.add("Book ID");
		     headers.add("ID");
		     headers.add("Title");
	      
	        final JTable table = new JTable( data, headers)
	        {
	               public boolean isCellEditable(int data, int columns)
	               {
	                   if(columns>=0)
	            	     return false;
	                   else return true;
	               }
	        };
	   
	     
	        table.addMouseListener(new MouseAdapter()    
	        {   
	        
	        	public void mouseClicked(MouseEvent evt)   
	        	{   
	        		    int col = table.getSelectedColumn();   
	        			int row = table.getSelectedRow();
	        		
	        			boolean is=table.isColumnSelected(0);
	        		
	        			if (!is){
	        			
	        				
	        				System.out.println("wrong selection");
	        				JOptionPane.showMessageDialog(null, "Please Select only Book ID");

	        			}
	        			else{	Object o=((String)table.getValueAt(row, col));
	        					bookid=o.toString();
	        					System.out.println(""+o);
	        					System.out.println("bookid:"+bookid);
	        			}
	        		}   
	        	}); 
	        

	        table.setFillsViewportHeight(true);
	        table.setRowHeight(20);
	        table.setPreferredScrollableViewportSize(new Dimension(900,150));
	        JScrollPane scrollPane = new JScrollPane(table);
	        
	        JButton btnLogout = new JButton("Logout");
	        btnLogout.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					ArrayList<String> Arr=new ArrayList<String>();
					Arr.add("Logout");
					Arr.add("Worker");
					Arr.add(LoginInfo.UserName);
					LibaryMain.chat.handleMessageFromGui(Arr);
					MainScreen.cardLayot.show(MainScreen.card, "Login");
					
				}
			});
			
	        JButton btnBack = new JButton("Back");
			
	        btnBack.addActionListener(new java.awt.event.ActionListener() {     
	            public void actionPerformed(java.awt.event.ActionEvent ev) {
	            	btnback(ev);
	            					
				}

				private void btnback(ActionEvent ev) {
					MainScreen.cardLayot.show(MainScreen.card, "viewdetail");
					
				}
	        });		
	        
	        JLabel lblPleaseSelectBook = new JLabel("Please Select Book ID And Press Extend ");
	        lblPleaseSelectBook.setFont(new Font("Tahoma", Font.BOLD, 15));
	        
	        JButton btnExtend = new JButton("Extend");
	        btnExtend.addActionListener(new java.awt.event.ActionListener() {     
	            public void actionPerformed(java.awt.event.ActionEvent ev) {
	            	btnExtendaction(ev);
	            					
				}

				public void btnExtendaction(ActionEvent ev) {
					
					 ArrayList<String> Arr=new ArrayList<String>();
			         Arr.add("MenualExtend");
			         Arr.add(bookid);
			         Arr.add(subid);
			         String ss=LoginInfo.UserName;
			         System.out.println("librarian name:"+ss);
			         Arr.add(ss);
			         LibaryMain.chat.handleMessageFromGui(Arr);
				
				}
			});
	       
	        GroupLayout groupLayout = new GroupLayout(this);
	        groupLayout.setHorizontalGroup(
	        	groupLayout.createParallelGroup(Alignment.LEADING)
	        		.addGroup(groupLayout.createSequentialGroup()
	        			.addContainerGap()
	        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
	        				.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 482, GroupLayout.PREFERRED_SIZE)
	        				.addComponent(btnBack, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
	        				.addComponent(lblPleaseSelectBook))
	        			.addPreferredGap(ComponentPlacement.RELATED)
	        			.addComponent(btnLogout, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
	        			.addContainerGap())
	        		.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
	        			.addContainerGap(302, Short.MAX_VALUE)
	        			.addComponent(btnExtend)
	        			.addGap(224))
	        );
	        groupLayout.setVerticalGroup(
	        	groupLayout.createParallelGroup(Alignment.LEADING)
	        		.addGroup(groupLayout.createSequentialGroup()
	        			.addGap(19)
	        			.addComponent(lblPleaseSelectBook)
	        			.addGap(69)
	        			.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	        			.addPreferredGap(ComponentPlacement.UNRELATED)
	        			.addComponent(btnExtend)
	        			.addGap(162)
	        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
	        				.addComponent(btnBack)
	        				.addComponent(btnLogout))
	        			.addContainerGap())
	        );
	        setLayout(groupLayout);
	        

		    }
	private Vector<Object> creatrow(String[] str, int i) {
		 Vector<Object> row = new Vector<Object>();
		 row.add( ""+str[i]);
		 row.add( ""+str[i+1]);
		 row.add( ""+str[i+2]);

		 	

		 			
		return row;
	}
}
